﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace HashMe
{
    public partial class HashMeNow : Form
    {
        public Boolean FipsEnabled;
        public RegistryKey PathKey 
        {
            get
            {
                return Registry.LocalMachine.OpenSubKey("SYSTEM").
                  OpenSubKey("CurrentControlSet").OpenSubKey("Control").
                  OpenSubKey("Lsa").OpenSubKey("FipsAlgorithmPolicy",true);
            }
        }

        public HashMeNow()
        {
            InitializeComponent();
            try
            {
                FipsEnabled = Convert.ToBoolean(PathKey.GetValue("Enabled"));
            }
            catch (Exception)
            {
                FipsEnabled = true;
            }
            
        }

        private void fillcmbHashTypes()
        {

            cmbHashTypes.Items.Add("SHA1");
            cmbHashTypes.Items.Add("SHA256");
            cmbHashTypes.Items.Add("SHA384");
            cmbHashTypes.Items.Add("SHA512");
            if (FipsEnabled == false)
            {
                cmbHashTypes.Items.Add("RIPEMD160");
                cmbHashTypes.Items.Add("MD5");
            }
            cmbHashTypes.SelectedIndex = 0;
        }

        private void fillcmbEncryptTypes()
        {
            cmbEncryptTypes.Items.Add("Rijndael");
            cmbEncryptTypes.SelectedIndex = 0;
        }

        private void fillcmbDecryptTypes()
        {
            cmbDecryptTypes.Items.Add("Rijndael");
            cmbDecryptTypes.SelectedIndex = 0;
        }
        private void btnHash_Click(object sender, EventArgs e)
        {
            if (cmbHashTypes.SelectedItem == null)
                return;
            BackgroundWorkerClass workerClass = new BackgroundWorkerClass();
            workerClass.Hash = txtBoxHashInput.Text;
            workerClass.HashFunc = cmbHashTypes.SelectedItem.ToString();
            backgroundWorker1.RunWorkerAsync(workerClass);
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            FormAbout formAbout = new FormAbout();
            formAbout.Visible = true;
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            BackgroundWorkerClass workerClass = e.Argument as BackgroundWorkerClass;
            workerClass.HashReturn = HashTypes.GetHash(workerClass.HashFunc, workerClass.Hash);
            e.Result = workerClass;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            BackgroundWorkerClass result = e.Result as BackgroundWorkerClass;
            txtBoxHashOutput.Text = result.HashReturn;
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtBoxEncryptInput.Text)||String.IsNullOrEmpty(txtBoxEncryptPassphrase.Text)
                || cmbEncryptTypes.SelectedItem == null)
                return;
            saveFileDialog1.Filter = "Text|*.txt";
            saveFileDialog1.Title = "Save the encrypted text in a file.";
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != "")
            {
                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:
                        using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                        {

                            try
                            {
                                sw.Write(EncryptTypes.GetEncrypt(cmbEncryptTypes.SelectedItem.ToString(),
                                        txtBoxEncryptInput.Text,
                                        txtBoxEncryptPassphrase.Text));
                            }
                            catch (Exception)
                            {
                            }
                            finally
                            {
                                sw.Close();
                            }
                            
                        }
                        break;

                }

                
            }
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtBoxEncryptPassphrase.Text))
            {
                MessageBox.Show("Zur Sicherheit sollte Eine Passphrase eingegeben werden das der verschlüsselte Text sicherer wird");
                return;
            }
            if (cmbEncryptTypes.SelectedItem == null)
            return;
            
            String result = EncryptTypes.GetEncrypt(cmbEncryptTypes.SelectedItem.ToString(),
                txtBoxEncryptInput.Text,
                txtBoxEncryptPassphrase.Text);
            txtBoxEncryptOutput.Text = result;
        }

        private void HashMeNow_Load(object sender, EventArgs e)
        {
            if (FipsEnabled == true)
                MessageBox.Show("Auf ihrem Pc ist der Fips 140 Standart aktiviert und lässt deshalb einige Verschlüsselungen und Hashfunktionen nicht zu."); 

            fillcmbHashTypes();
            fillcmbEncryptTypes();
            fillcmbDecryptTypes();
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtBoxDecryptPassphrase.Text))
            {
                MessageBox.Show("Zur Sicherheit sollte Eine Passphrase eingegeben werden das der verschlüsselte Text sicherer wird");
                return;
            }
            if (cmbDecryptTypes.SelectedItem == null)
                return;
            try
            {
                String result = DecryptTypes.GetDecrypt(cmbDecryptTypes.SelectedItem.ToString(),
                    txtBoxDecryptInput.Text,
                    txtBoxDecryptPassphrase.Text);
                txtBoxDecryptOutput.Text = result;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            
        }

        private void tsmFipsActivate_Click(object sender, EventArgs e)
        {
            if (FipsEnabled == true)
                return;
            if (FipsEnabled == false)
            {
                PathKey.SetValue("Enabled", 1);
                FipsEnabled = true;
            }
        }

        private void tsmFipsDeactivate_Click(object sender, EventArgs e)
        {
            if (FipsEnabled == false)
                return;
            if (FipsEnabled == true)
            {
                PathKey.SetValue("Enabled", 0);
                FipsEnabled = false;
            }
        }




    }
}
